<?php
namespace app\common\model;

use app\common\model\ManagerRole as ManagerRoleModel;

class Channel extends Base
{
    protected $table = 'channels';

    protected static function init()
    {

    }
    
    public function getChannelTypeLabelAttr($value, $data)
    {
        $result = '';
        //page_view,user_view,register
        switch ($data['channel_type']) {
            case 1:
                $result = '平台链接推广';
                break;
            case 2:
                $result = 'api接口推广';
                break;
           
        }

        return $result;
    }

    public function getTypeLabelAttr($value, $data)
    {
        $result = '';
        //page_view,user_view,register
        switch ($data['type']) {
            case 'page_view':
                $result = 'PV';
                break;
            case 'user_view':
                $result = 'UV';
                break;
            case 'register':
                $result = '注册';
                break;
			case 'user_push':
                $result = '进件';
                break;
        }

        return $result;
    }

    public function getCreateTimeLabelAttr($value, $data)
    {
        if (empty($data['create_time'])) {

            return '';
        }

        return date('Y-m-d H:i:s', $data['create_time']);
    }

    public function getShareUrlAttr($value, $data)
    {
        if (is_https()) {
            $http = 'https';
        } else {
            $http = 'http';
        }

        $share_url = $http . '://' . $_SERVER['HTTP_HOST'] . '/channel.html?code=' . $data['channel_code'];

        return $share_url;
    }

    public function getPageViewCountAttr($value, $data)
    {
        $count = PageViewLog::where('channel_id', '=', $data['id'])->count();

        return $count;
    }

    public function getUserViewCountAttr($value, $data)
    {
        $count = UniqueViewLog::where('channel_id', '=', $data['id'])->count();

        return $count;
    }

    public function getRegisterCountAttr($value, $data)
    {
        $count = Member::where('channel_id', $data['id'])->count();

        return $count;
    }

    public function getUserViewNumberLabelAttr($value, $data)
    {
        if ($data['user_view_number'] == -1) {
            return '无限制';
        }

        return $data['user_view_number'];
    }
}